package server.fx;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TextArea;
import javafx.scene.text.Text;
import server.socket.SocketServer;

public class ServerController implements Initializable {

	@FXML
	private ListView<String> clientListView = new ListView<>();// 客户列表
	@FXML
	private TextArea sendArea = new TextArea("");// 发送框
	@FXML
	private Button sendBtn = new Button();// 发送按钮
	@FXML
	private TextArea receArea = new TextArea();// 接收框
	@FXML
	private Button startBtn = new Button();// 启动按钮
	@FXML
	private Button closeBtn = new Button();// 关闭按钮
	@FXML
	private Text tipLabel = new Text();// 提示框

	private ObservableList<String> clientList = FXCollections.observableArrayList();
	private List<String> ipList = new ArrayList<>();// 选中的客户列表

	private SocketServer server;// 服务器
	private SimpleStringProperty receMsg = new SimpleStringProperty();// 接收到的消息

	@Override
	public void initialize(URL location, ResourceBundle resources) {
		this.server = new SocketServer(receMsg, clientList);
		clientListView.setItems(clientList);
		sendBtn.setDisable(true);
		sendArea.setDisable(true);
		closeBtn.setDisable(true);
		receMsg.addListener(e -> {// 监听接收消息
			receArea.setText(receArea.getText() + receMsg.get() + "\n");
		});
		sendArea.textProperty().addListener(e -> {// 监听发送框
			if (sendArea.getText().trim().equals("")) {
				sendBtn.setDisable(true);
			} else {
				sendBtn.setDisable(false);
			}
		});

		clientList.addListener((ListChangeListener<String>) change -> {// 监听客户列表
			Platform.runLater(() -> {
				tipLabel.setText(clientList.size() + " Connetced");
			});
		});

		clientListView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
		// 监听选中的客户
		clientListView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
			@Override
			public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
				ipList.clear();
				ipList.addAll(clientListView.getSelectionModel().getSelectedItems());
			}

		});

	}

	@FXML
	private void sendBtnOnAction(ActionEvent e) {
		Platform.runLater(() -> {
			server.sendMessage(ipList, sendArea.getText());
		});
	}

	@FXML
	private void startBtnOnAction(ActionEvent e) {
		Platform.runLater(() -> {
			if (server.start()) {
				startBtn.setDisable(true);
				closeBtn.setDisable(false);
				sendArea.setDisable(false);
			}
		});
	}

	@FXML
	private void closeBtnOnAction(ActionEvent e) {
		Platform.runLater(() -> {
			if (server.close()) {
				closeBtn.setDisable(true);
				startBtn.setDisable(false);
				sendArea.setDisable(true);
			}
		});
	}

}
